package com.zhugy.comparecode;

import java.util.List;

import com.zhugy.comparecode.config.CompareConfig;
import com.zhugy.comparecode.util.CommonUtils;

public abstract class SingleLineComparableFile extends BaseCodeFile {
	
	public SingleLineComparableFile(String baseDir, String relativePath) {
		super(baseDir, relativePath);
	}

	@Override
	public CompareResult compareTo(CodeFile cf, CompareConfig config) {
		if (cf.getClass() != this.getClass()) {
			return CompareResult.fail(getShowName() + "与" + cf.relativeName() + "不相同，类型不一样。" + getShowName() + "："
					+ getClass().getSimpleName() + "   " + cf.relativeName() + ":" + cf.getClass().getSimpleName());
		}
		List<CodeLine> lines1 = this.lines();
		List<CodeLine> lines2 = cf.lines();
		
		int size1 = CommonUtils.size(lines1);
		int size2 = CommonUtils.size(lines2);

		for (int i = 0; i < size1; i++) {
			if (i >= size2) {
				String msg = getShowName() + "的还剩下代码行但" + cf.getShowName() + "已经没有了";
				return CompareResult.fail(msg);
			}
			CodeLine line1 = lines1.get(i);
			CodeLine line2 = lines2.get(i);
			CompareResult cr = line1.compareTo(line2);
			if (!cr.isEquals()) {
				String msg = getShowName() + "的第" + line1.index() + "行与" + cf.getShowName() + "的第" + line2.index()
						+ "行存在差异：" + cr.getMessage();
				if (config.isIgnoreLine1(relativeName(), line1.index()) || config.isIgnoreLine2(cf.relativeName(), line2.index())) {
					System.out.println(msg + "但忽略之");
				} else {
					return CompareResult.fail(msg);
				}
			}
		}
		return CompareResult.success("");
	}


}
